package com.literature.threeGroup.service.impl;

import com.literature.threeGroup.mapper.*;
import com.literature.threeGroup.pojo.*;
import com.literature.threeGroup.service.MenuService;
import com.literature.threeGroup.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;

/**
 * @author VULCAN
 * @create 2020-01-07 14:30
 * @desc
 **/
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private MenuRoleMapper menuRoleMapper;
    @Autowired
    private OrganMapper organMapper;
    @Autowired
    private ParamMapper paramMapper;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogMapper logMapper;
    @Override
    public List<Menu> treeMenu(String id) {
        List<Menu> findall = menuMapper.treeMenu(id);
        return findall;
    }


    /**
      * @name  角色menu
     * @Author VULCAN
     * @Date 2020/1/10 8:41
     */
    @Override
    public void insertrolemenu(MenuRole menuRole) {
        menuRoleMapper.insert(menuRole);
    }

    /**
      * @name  tree
     * @Author VULCAN
     * @Date 2020/1/10 8:47
     */
    @Override
    public List<Menu> findmenu() {
        return menuMapper.findmenu();
    }

    /**
      * @name  机构tree
     * @Author VULCAN
     * @Date 2020/1/11 10:00
     */
    @Override
    public List<Organ> findorgantree() {
        return organMapper.findorgantree();
    }

    /**
      * @name  参数tree
     * @Author VULCAN
     * @Date 2020/1/11 9:59
     */
    @Override
    public List<Param> findparamtree() {
        return paramMapper.findparamtree();
    }


    /**
     * @name  机构列表 + 模糊
     * @Author VULCAN
     * @Date 2020/1/22 13:16
     */
    @Override
    public List<Organ> findorgantreelikename(Organ organ) {
        return organMapper.findorgantreelikename(organ);
    }
    /**
     * @name  参数列表 +模糊
     * @Author VULCAN
     * @Date 2020/1/22 13:19
     */
    @Override
    public List<Param> findparamtreelikename(Param param) {
        return paramMapper.findparamtreelikename(param);
    }
    /**
     * @name  机构删除
     * @Author VULCAN
     * @Date 2020/1/23 12:45
     */
    @Override
    public void organdel(String id) throws UnknownHostException {
        Organ organ = organMapper.selectByPrimaryKey(id);
        organMapper.organdel(id);

        //记录日志
        Log log = new Log();
        log.setLogId(idWorker.nextId()+"");
        log.setLogDate(new Date());
        String login = (String) redisTemplate.opsForValue().get("login");
        log.setLogName(login);
        InetAddress ip4 = Inet4Address.getLocalHost();
        log.setLogIp(ip4.getHostAddress());
        log.setLogContent("机构删除-"+organ.getName()+"！！");
        log.setLogType("机构删除");
        logMapper.insert(log);
    }
    /**
     * @name  机构添加
     * @Author VULCAN
     * @Date 2020/1/23 12:45
     */
    @Override
    public void organadd(Organ organ) throws UnknownHostException {
        organ.setId(idWorker.nextId()+"");
        String nextId = idWorker.nextId()+"";
        organ.setOrganCode(nextId.substring(16));
        organMapper.insert(organ);

        //记录日志
        Log log = new Log();
        log.setLogId(idWorker.nextId()+"");
        log.setLogDate(new Date());
        String login = (String) redisTemplate.opsForValue().get("login");
        log.setLogName(login);
        InetAddress ip4 = Inet4Address.getLocalHost();
        log.setLogIp(ip4.getHostAddress());
        log.setLogContent("机构添加-"+organ.getName()+"！！");
        log.setLogType("机构添加");
        logMapper.insert(log);
    }
    /**
     * @name  机构修改
     * @Author VULCAN
     * @Date 2020/1/23 12:45
     */
    @Override
    public void organupdate(Organ organ) throws UnknownHostException {
        organMapper.organupdate(organ);

        //记录日志
        Log log = new Log();
        log.setLogId(idWorker.nextId()+"");
        log.setLogDate(new Date());
        String login = (String) redisTemplate.opsForValue().get("login");
        log.setLogName(login);
        InetAddress ip4 = Inet4Address.getLocalHost();
        log.setLogIp(ip4.getHostAddress());
        log.setLogContent("机构修改-"+organ.getName()+"！！");
        log.setLogType("机构修改");
        logMapper.insert(log);
    }
    /**
     * @name  参数删除
     * @Author VULCAN
     * @Date 2020/1/23 12:45
     */
    @Override
    public void paramdel(String id) throws UnknownHostException {
        Param param = paramMapper.selectByPrimaryKey(id);
        paramMapper.paramdel(id);

        //记录日志
        Log log = new Log();
        log.setLogId(idWorker.nextId()+"");
        log.setLogDate(new Date());
        String login = (String) redisTemplate.opsForValue().get("login");
        log.setLogName(login);
        InetAddress ip4 = Inet4Address.getLocalHost();
        log.setLogIp(ip4.getHostAddress());
        log.setLogContent("参数删除-"+param.getName()+"！！");
        log.setLogType("参数删除");
        logMapper.insert(log);
    }
    /**
     * @name  参数修改
     * @Author VULCAN
     * @Date 2020/1/23 12:45
     */
    @Override
    public void paramupdate(Param param) throws UnknownHostException {
        paramMapper.paramupdate(param);
        //记录日志
        Log log = new Log();
        log.setLogId(idWorker.nextId()+"");
        log.setLogDate(new Date());
        String login = (String) redisTemplate.opsForValue().get("login");
        log.setLogName(login);
        InetAddress ip4 = Inet4Address.getLocalHost();
        log.setLogIp(ip4.getHostAddress());
        log.setLogContent("参数修改-"+param.getName()+"！！");
        log.setLogType("参数修改");
        logMapper.insert(log);
    }
    /**
     * @name  参数添加
     * @Author VULCAN
     * @Date 2020/1/23 12:45
     */
    @Override
    public void paramadd(Param param) throws UnknownHostException {
        param.setId(idWorker.nextId()+"");
        paramMapper.insert(param);
        //记录日志
        Log log = new Log();
        log.setLogId(idWorker.nextId()+"");
        log.setLogDate(new Date());
        String login = (String) redisTemplate.opsForValue().get("login");
        log.setLogName(login);
        InetAddress ip4 = Inet4Address.getLocalHost();
        log.setLogIp(ip4.getHostAddress());
        log.setLogContent("参数添加-"+param.getName()+"！！");
        log.setLogType("参数添加");
        logMapper.insert(log);
    }
    /**
     * @name  organcount
     * @Author VULCAN
     * @Date 2020/1/11 10:01
     */
    @Override
    public List organcount() {
        return organMapper.organcount();
    }
    /**
     * @name  paramcount
     * @Author VULCAN
     * @Date 2020/1/11 10:01
     */
    @Override
    public List paramcount() {
        return paramMapper.paramcount();
    }
    /**
     * @name  学科
     * @Author VULCAN
     * @Date 2020/2/1 18:18
     */
    @Override
    public List<Param> findparamsubject() {
        return paramMapper.findparamsubject();
    }
    /**
     * @name  国家
     * @Author VULCAN
     * @Date 2020/2/1 18:18
     */
    @Override
    public List<Param> findparamcountry() {
        return paramMapper.findparamcountry();
    }
    /**
     * @name  文献
     * @Author VULCAN
     * @Date 2020/2/1 18:18
     */
    @Override
    public List<Param> findparamliterature() {
        return paramMapper.findparamliterature();
    }
    /**
     * @name  载体
     * @Author VULCAN
     * @Date 2020/2/1 18:18
     */
    @Override
    public List<Param> findparamcmorphology() {
        return paramMapper.findparamcmorphology();
    }
    /**
     * @name  语种
     * @Author VULCAN
     * @Date 2020/2/1 18:18
     */
    @Override
    public List<Param> findparamlanguage() {
        return paramMapper.findparamlanguage();
    }
    /**
     * @name  学科级别下拉
     * @Author VULCAN
     * @Date 2020/2/8 13:00
     */
    @Override
    public List<Param> findparamsubjectXl() {
        return paramMapper.findparamsubjectXl();
    }
    /**
     * @name  语种级别下拉
     * @Author VULCAN
     * @Date 2020/2/8 13:00
     */
    @Override
    public List<Param> findparamlanguageXl() {
        return paramMapper.findparamlanguageXl();
    }

}
